//
//  VIZStatueCell.swift
//  VIZ微博
//
//  Created by 位立志 on 16/4/13.
//  Copyright © 2016年 位立志. All rights reserved.
//

import UIKit
import SnapKit

class VIZStatueCell: UITableViewCell {

    //cell的工具条视图
    private lazy var cellToolBar : VIZCellToolBar = VIZCellToolBar()
    
    //转发微博
    private lazy var retweetedStatue : VIZHMStatusRetweetedView = VIZHMStatusRetweetedView()
    
    //定义toolBar的顶部约束
    var toolBarTop : Constraint?
    
    //模型
    var cellViewModel : VIZCellViewModel? {
        didSet{
            
            //输出图片信息
            print(cellViewModel?.homeStatuse?.pic_urls?.count)
            
            originalView.cellViewModel = cellViewModel
            cellToolBar.cellViewModel = cellViewModel
//            retweetedStatue.cellViewModel = cellViewModel
            //更新约束前卸载toolBar的顶部约束
            self.toolBarTop?.uninstall()
            //转发微博
            if cellViewModel?.homeStatuse?.retweeted_status != nil {
                //更新顶部视图
                cellToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTop = make.top.equalTo(retweetedStatue.snp_bottom).constraint
                })
                retweetedStatue.hidden = false
                retweetedStatue.cellViewModel = cellViewModel
                
            }
            
            //更改原创微博
            else {
                cellToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.toolBarTop = make.top.equalTo(self.originalView.snp_bottom).constraint
                    
                })
                retweetedStatue.hidden = true
                retweetedStatue.cellViewModel = nil
            }
            
        }
    }
    
    //懒加载控件
    lazy var originalView:VIZOriginalView = VIZOriginalView()
    
    //重新init
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .Default, reuseIdentifier: reuseIdentifier)
        //设置选中的颜色
        selectionStyle = .None
        
        //输出图片信息
        print(cellViewModel?.homeStatuse?.pic_urls?.count)
        
        setUI()
    }
    
    private func setUI(){
        
        backgroundColor = UIColor.whiteColor()
        contentView.addSubview(originalView)
        contentView.addSubview(cellToolBar)
        contentView.addSubview(retweetedStatue)
        //添加原始视图的约束
        originalView.snp_makeConstraints { (make) -> Void in
            
            make.left.right.top.equalTo(contentView)
            //make.height.equalTo(100)
        }
        
        //添加转发微博正文
        retweetedStatue.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(originalView)
            make.right.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom).offset(8)

            
        }
        //添加toolBar
        cellToolBar.snp_makeConstraints { (make) -> Void in
            
            self.toolBarTop = make.top.equalTo(retweetedStatue.snp_bottom).offset(8).constraint
            make.left.right.equalTo(self.contentView)
            make.height.equalTo(36)
            
        }
        
        
        //输出转发微博
        //print(retweetedStatue.textLabel?.text)
        
        //设置自身的约束:自动计算行高
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(cellToolBar.snp_bottom)
            make.top.left.right.equalTo(self)
        }
        
                
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
